﻿// --------------------------------------------------------------------------------------------------------------------
// <copyright file="ProductsVersionsSettingsManagerOptionsForm.cs" company="Roman '4ux-nbIx' Novitsky">
//   Copyright (c) Roman '4ux-nbIx' Novitsky 2009
// </copyright>
// <summary>
//   Describes and manipulates the products versions settings manager options form.
// </summary>
// --------------------------------------------------------------------------------------------------------------------

namespace SettingsManager.ComponentSettingsManagers
{
  #region Namespace Imports

  using System;
  using System.Collections.Generic;
  using System.Linq;
  using System.Windows.Forms;

  using JetBrains.Annotations;

  #endregion


  /// <summary>
  /// Describes and manipulates the products versions settings manager options form.
  /// </summary>
  public partial class ProductsVersionsSettingsManagerOptionsForm : SettingsManagerOptionsForm
  {
    #region Constants and Fields

    private List<string> _options;

    #endregion


    #region Constructors and Destructors

    /// <summary>
    /// Initializes a new instance of the <see cref="ProductsVersionsSettingsManagerOptionsForm"/> class.
    /// </summary>
    public ProductsVersionsSettingsManagerOptionsForm()
    {
      InitializeComponent();

      IEnumerable<string> products = GetProducts();

      foreach (string product in products)
      {
        _productsListBox.Items.Add(product);
      }
    }

    #endregion


    #region Properties

    /// <summary>
    /// Gets or sets the options.
    /// </summary>
    /// <value>
    /// The options.
    /// </value>
    public override object Options
    {
      get
      {
        return _options;
      }

      set
      {
        _options = value != null ? new List<string>((List<string>)value) : null;

        UpdateCheckBoxes();
      }
    }

    #endregion


    #region Methods

    [NotNull]
    private static IEnumerable<string> GetProducts()
    {
      var productsVersions = ProductsVersionsSettingsManager.GetProductsVersions();

      return productsVersions.Keys.OrderBy();
    }


    private void OnOkButtonClick([NotNull] object sender, [NotNull] EventArgs e)
    {
      _options = new List<string>();

      foreach (string item in _productsListBox.CheckedItems)
      {
        _options.Add(item);
      }

      DialogResult = DialogResult.OK;
    }


    private void UpdateCheckBoxes()
    {
      if (_options == null)
      {
        for (int i = 0; i < _productsListBox.Items.Count; i++)
        {
          _productsListBox.SetItemChecked(i, false);
        }

        return;
      }

      for (int i = 0; i < _productsListBox.Items.Count; i++)
      {
        var product = (string)_productsListBox.Items[i];

        _productsListBox.SetItemChecked(i, _options.Contains(product));
      }
    }

    #endregion
  }
}